<template>
  <div class="music-list" style="height: 100%">
    <div class="list-header">
      <span class="list-name">歌曲</span>
      <span class="list-art">歌手</span>
      <span class="list-time">专辑</span>
    </div>
    <div class="list-content" style="height: 92%">
      <el-scrollbar height="100%" always>
        <div
          v-for="(item, index) in searchList"
          class="list-item"
          :key="item.id"
        >
          <span
            class="list-num"
            v-if="!listStore.playing && listStore.music.id === item.id"
          >
            <img src="../../assets/img/wave.gif" />
          </span>
          <span class="list-num" v-else>{{ index + 1 }}</span>
          <div class="list-name">
            <span>{{ item.name }}</span>
            <div class="onOff" @click="handleOnOff(item)">
              <SvgIcon
                v-if="!listStore.playing && item.id === listStore.music.id"
                iconName="icon-Pause"
                class="play"
              ></SvgIcon>
              <SvgIcon v-else iconName="icon-play2" class="play2"></SvgIcon>
              <!-- <SvgIcon
                v-if="!listStore.playing && item.id !== listStore.music.id"
                iconName="icon-play2"
                class="play2"
              ></SvgIcon>
              <SvgIcon v-else iconName="icon-Pause" class="play2"></SvgIcon> -->
            </div>
          </div>
          <span class="list-art">{{
            item.artists?.[0]?.name || item.ar?.[0]?.name
          }}</span>
          <div class="list-time">
            <span>{{ item.album?.name || item.al?.name }}</span>
          </div>
        </div>
      </el-scrollbar>
    </div>
  </div>
</template>

<script setup>
import { defineProps } from "vue"
import { useListStore } from "../../store/list.js"
import { getLyric, getMusicUrl } from "../../api/index"

defineProps({
  searchList: {
    type: Array,
  },
})

const listStore = useListStore()

const play = () => {
  listStore.audioDom.play()
}

const pause = () => {
  listStore.audioDom.pause()
}

const handleOnOff = async (item) => {
  if (
    item.id === listStore.music.id ||
    Object.keys(listStore.music).length === 0
  ) {
    listStore.setPlaying()
  }
  let duration = item.duration || item.dt
  let date = new Date(duration)
  let m = date.getMinutes()
  let s = date.getSeconds()
  let time = `${m.toString().padStart(2, "0")}:${s.toString().padStart(2, "0")}`
  listStore.addHistory({
    album: item.album?.name || item.al?.name,
    duration: time,
    id: item.id,
    image: item.album?.artist.img1v1Url || item.al?.picUrl,
    name: item.name,
    singer: item.artists?.[0].name || item.ar?.[0]?.name,
  })
  if (item.id !== listStore.music.id) {
    let res = await getMusicUrl(item.id)
    listStore.setUrl(res.data.data[0].url)
    listStore.setMusic(item)
    const response = await getLyric(item.id)
    const tmp = response.data.lrc.lyric
    listStore.setLyric(tmp)
  }
  listStore.setCurrentTime(listStore.audioDom.currentTime)
  // listStore.setDuration(
  //   Number(listStore.music.duration?.split(":")[0]) * 60 +
  //     Number(listStore.music.duration?.split(":")[1])
  // )
  if (listStore.playing) {
    pause()
  } else {
    play()
  }
}
</script>

<style scoped lang="less">
.play2:hover,
.play:hover,
.delete:hover {
  cursor: pointer;
}
.onOff {
  display: flex;
  align-items: center;
  height: 100%;
}
.list-item:hover {
  .play2 {
    display: inline-block;
  }
}
.play2 {
  display: none;
}
::v-deep .el-scrollbar__bar.is-vertical {
  background-color: #514f4ff7;
  // margin-top: 16px;
}
::v-deep .el-scrollbar__thumb {
  background-color: #fff;
}
.list-header {
  display: flex;
  height: 50px;
  border-bottom: 1px solid hsla(0, 0%, 100%, 0.1);
  line-height: 50px;
  color: #fff;
  font-size: 14px;
  padding-left: 40px;
  .list-name {
    flex: 1;
    display: inline-block;
  }
  .list-art {
    width: 200px;
    display: inline-block;
  }
  .list-time {
    width: 200px;
    display: inline-block;
  }
}
.list-item {
  display: flex;
  height: 50px;
  border-bottom: 1px solid hsla(0, 0%, 100%, 0.1);
  line-height: 50px;
  color: hsla(0, 0%, 100%, 0.6);
  font-size: 14px;
  .list-num {
    display: flex;
    justify-content: center;
    align-items: center;
    height: 50px;
    line-height: 50px;
    width: 30px;
    margin-right: 10px;
    img {
      width: 10px;
      height: 10px;
    }
  }
  .list-name {
    flex: 1;
    display: flex;
    justify-content: space-between;
    align-items: center;
    .play2 {
      margin-right: 5px;
      width: 35px;
      height: 35px;
      cursor: pointer;
    }
    .play {
      margin-right: 5px;
      width: 30px;
      height: 30px;
    }
  }
  .list-art {
    width: 200px;
    display: inline-block;
  }
  .list-time {
    span {
      width: 200px;
      display: block;
      align-items: center;
      text-overflow: ellipsis;
      overflow: hidden;
      white-space: nowrap;
      margin-right: 6px;
    }
  }
}
</style>
